Basic- TCP-Proxy

I found there are lots of redundant code blocks, so I reuse it by define it as:

def receive_and_send(client_socket, target_socket, handler):

    client_buffer = receive_from(client_socket)
    if not len(client_buffer):
        return
    hexdump(client_buffer)

    client_buffer = handler(client_buffer)

    if len(client_buffer):
        print 'Sending:%s bytes' % len(client_buffer)
        target_socket.send(client_buffer)

    if not len(client_buffer): # No more data
        return False
    return True

The third argument is the function reference which points to response_handler or request_handler in this book (or any other function reference that you create for your own purpose)

In the end, it return False/True to represent whether there is more data or not. So, to use it is quite simple:

receive from local and send it to remote host.

has_more_data_to_send = receive_and_send(local_socket, remote_socket, response_handler)

So after that, we can choose to close the socket if there is no more data to send.To receive from remote and send to local is simple, we just switch local_socket and remote_socket, and change from reeponse_handler to request_handler.

In the code block 5, there is a bug:

if not len(local_buffer) or not len(remote_buffer)

we should change it to:

if not len(local_buffer) and not len(remote_buffer)

otherwise if one of them has more data to send, it will break.

To show how the code works, I install vsftpd (ftp server) on another vm. The user/password of the ftp server is the non-root user/password of the host os.

After executing this code and logining ftp on kali host, I found 'Bad file descriptor' error.

This is the socket error when sending to a closed socket. So I find I miss the code 'break' in

if not len(local_buffer) and not len(remote_buffer)

code block. After adding that 'break', the code work properly.

Also, I use telnet 23 port to login to a remote bbs by the proxy (since this is on TCP):

results matching ""

    No results matching ""